/*
 * @Author: wusd
 * @Date: 2022-06-15 09:39:58
 * @LastEditors: wusd
 * @LastEditTime: 2022-06-15 16:19:34
 * @Description:
 *
 */
import store from '@/store'
import { getQueryVariable } from '@/untils'
import { AnyObj, MenueItem } from 'types/common'

//得到菜单
async function getMenue() {
  let menue = []
  function fixMenue(arr: string | any[]) {
    for (let i = 0; i < arr.length; i++) {
      //菜单组
      if (arr[i].type === 1) {
        if (arr[i]?.children?.length > 0) {
          fixMenue(arr[i].children)
        }
      }
      //菜单
      if (arr[i].type === 2) {
        const query = getQueryVariable(arr[i].path)
        arr[i].query = query
        //解析路由参数
        // if (query) {
        //   let basePath = arr[i].path.substring(0, arr[i].path.indexOf('?'))
        //   let windowpath = basePath
        //   for (const key in query) {
        //     basePath = basePath + '/:' + key
        //     windowpath = windowpath + '/' + query[key]
        //   }
        //   arr[i].path = basePath
        //   arr[i].windowpath = windowpath
        // }
        //处理按钮权限
        const btn: AnyObj = {}
        arr[i]?.children?.forEach((item: AnyObj) => {
          if (item.type === 3) {
            const type: string = item.path.split('_')[1]
            btn[type] = true
          }
        })
        arr[i].btn = btn
        delete arr[i]?.children
      }
    }
  }
  const list: Array<MenueItem> = [
    {
      text: '个人',
      path: '/personal',
      pageUrl: '/personal',
      name: 'personal',
      id: '1',
      type: 2,
      icon: 'icon',
      children: [
        {
          path: 'personal_add',
          text: '新增',
          type: 3
        }
      ]
    },
    {
      text: '用户管理',
      path: '/userSys',
      pageUrl: '/userSys',
      name: 'userSys',
      id: '2',
      type: 2
    }
  ]
  menue = JSON.parse(JSON.stringify(list))
  fixMenue(menue)
  store.commit('setMenue', menue)
  store.dispatch('addMenue')
}
export { getMenue }
